
from odoo import api, fields, models, _


class AccLine(models.Model):
    _inherit = 'pre.account.line'

    sign_total_quantity = fields.Float(compute="_compute_sign_total_quantity", store=True,
                                       string="总数量(带正负号)")
    sign_invoiced_quantity = fields.Float(compute="_compute_show_info", store=True,
                                          string="已开票数量(带正负号)")
    sign_invoiced_amount_tax = fields.Float(compute="_compute_show_info", store=True,
                                            string="已开票金额(带正负号)")
    sign_uninvoiced_amount_tax = fields.Float(compute="_compute_show_info", store=True,
                                              string="未开票金额(带正负号)")
    sign_business_amount = fields.Float('总金额(含税)(带正负号)', store=True,
                                        compute="_compute_sign_business_amount")
    show_product_name = fields.Char('产品名称', related="product_id.name", store=True)
    show_nd_size = fields.Char('产品规格', related="product_id.nd_size", store=True)

    @api.depends('quantity')
    def _compute_sign_total_quantity(self):
        for line in self:
            if line.type in ['sale_return', 'purchase_return']:
                line.sign_total_quantity = -line.quantity
            else:
                line.sign_total_quantity = line.quantity

    @api.depends('invoiced_qty', 'invoiced_amount_tax', 'business_amount', 'state')
    def _compute_show_info(self):
        for line in self:
            if line.type in ['sale_return', 'purchase_return']:
                data = {
                    'sign_invoiced_quantity': -line.invoiced_qty,
                    'sign_invoiced_amount_tax': -line.invoiced_amount_tax,
                    'sign_uninvoiced_amount_tax': -(line.business_amount - line.invoiced_amount_tax)
                }
                # 已开票或者无需开票的，未开票金额强制显示0（即使开票金额 不等于 销售金额，仍然显示0）
                if line.state in ['invoiced', 'free']:
                    data['sign_uninvoiced_amount_tax'] = 0
            else:
                data = {
                    'sign_invoiced_quantity': line.invoiced_qty,
                    'sign_invoiced_amount_tax': line.invoiced_amount_tax,
                    'sign_uninvoiced_amount_tax': line.business_amount - line.invoiced_amount_tax,
                }
                if line.state in ['invoiced', 'free']:
                    data['sign_uninvoiced_amount_tax'] = 0
            line.update(data)

    @api.depends('business_amount')
    def _compute_sign_business_amount(self):
        for line in self:
            if line.type in ['sale_return', 'purchase_return']:
                line.sign_business_amount = -line.business_amount
            else:
                line.sign_business_amount = line.business_amount

